package com.atguigu.mock.util;

import org.apache.commons.lang3.time.DateUtils;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class MyDateUtil {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2022,0,25,1,2);
        System.out.println(calendar.getTime());

    }

    public static Date  addHours(Date curDate,Integer hour ,boolean inWorkTime){
        Date date = DateUtils.addHours(curDate, hour);
        int curHour = getHour(date);
        if(inWorkTime){
            if(curHour<9  ){
                date = DateUtils.addHours(date, 9-curHour);
            }else if((curHour >19  )){
                date = DateUtils.addHours(date, 9+24-curHour);
            }
        }
        return  date;
    }

    public static Date  initDate(String date,int hour,int minute){
        Calendar calendar = Calendar.getInstance();
        String[] dateStrs = date.split("-");
        int year = Integer.valueOf(dateStrs[0]);
        int month = Integer.valueOf(dateStrs[1])-1;
        int dt = Integer.valueOf(dateStrs[2]);
        calendar.set(year,month,dt,hour,minute);
        return calendar.getTime();
    }

    public static int   getYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return   calendar.get(Calendar.YEAR);
    }

    public static int   getHour(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return   calendar.get(Calendar.HOUR);
    }




    public static int getBetweenYear(Date begin, Date end) {
        LocalDate beginL = begin.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endL = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Period p = Period.between(beginL, endL);
        int years = p.getYears();
        return years;
    }



}
